// https://leetcode.cn/problems/same-tree/solution/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */

// 法一：DFS
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if (!p && !q) return true;
        if (!p || !q) return false;
        return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    }
};

// 法二：BFS
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        queue<TreeNode*> treeQueue;
        treeQueue.push(p);
        treeQueue.push(q);
        while (!treeQueue.empty()) {
            TreeNode* ptmp = treeQueue.front();
            treeQueue.pop();
            TreeNode* qtmp = treeQueue.front();
            treeQueue.pop();
            if (ptmp == NULL && qtmp == NULL) continue;
            if (ptmp == NULL || qtmp == NULL || ptmp->val != qtmp->val) return false;
            treeQueue.push(ptmp->left);
            treeQueue.push(qtmp->left);
            treeQueue.push(ptmp->right);
            treeQueue.push(qtmp->right);
        }
        return true;
    }
};